﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text.RegularExpressions;
using System.Text;

namespace Com.Rivyer.Web.ip
{
    public partial class ping : System.Web.UI.Page
    {
        private readonly BLL.ipaddressBll ipbll = new BLL.ipaddressBll();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.Params["ping"] != null
                && Request.Params["ping"].Trim().Length != 0)
                {
                    string pinglist = Request.Params["ping"];
                    List<Model.ipaddressModel> ipmodels = ipbll.GetModelList(string.Format("id in ({0})", pinglist));
                    //单ping
                    if (ipmodels.Count == 1)
                    {
                        ASPxTextBox1.Text = ipmodels[0].ip;
                        ASPxButton3_Click(ASPxButton3, new EventArgs());

                    }
                    else
                    {
                        //批量
                        ASPxRadioButtonList2.Items[1].Selected = true;
                        ASPxTextBox1.Visible = false;
                        ASPxMemo1.Visible = true;
                        StringBuilder sb = new StringBuilder();

                        foreach (Model.ipaddressModel ipmodel in ipmodels)
                        {
                            sb.Append(ipmodel.ip + "\r\n");

                        }
                        ASPxMemo1.Text = sb.ToString();
                        ASPxButton3_Click(ASPxButton3, new EventArgs());

                    }
                }

                if (Request.Params["mac"] != null
                   && Request.Params["mac"].Trim().Length != 0)
                {
                    string pinglist = Request.Params["mac"];
                    List<Model.ipaddressModel> ipmodels = ipbll.GetModelList(string.Format("id in ({0})", pinglist));
                    //单mac
                    if (ipmodels.Count == 1)
                    {
                        ASPxTextBox1.Text = ipmodels[0].ip;
                        ASPxButton4_Click(ASPxButton4, new EventArgs());
                        ASPxTextBox1.Text = ipmodels[0].ip + "  原MAC:" + ipmodels[0].macaddress + "  MAC匹配:" + (ASPxMemo2.Text.IndexOf(ipmodels[0].macaddress == null ? "*" : ipmodels[0].macaddress) > 0 ? "是" : "否");
                    }
                    else
                    {
                        //批量
                        ASPxRadioButtonList2.Items[1].Selected = true;
                        ASPxTextBox1.Visible = false;
                        ASPxMemo1.Visible = true;
                        StringBuilder sb = new StringBuilder();
                        StringBuilder sb1 = new StringBuilder();
                        foreach (Model.ipaddressModel ipmodel in ipmodels)
                        {
                            sb.Append(ipmodel.ip + "\r\n");
                            // sb1.Append(ipmodel.ip + "  原MAC:" + ipmodel.macaddress + "\r\n");
                        }
                        ASPxMemo1.Text = sb.ToString();
                        ASPxButton4_Click(ASPxButton4, new EventArgs());
                        foreach (Model.ipaddressModel ipmodel in ipmodels)
                        {
                            sb1.Append(ipmodel.ip + "  原MAC:" + ipmodel.macaddress + "  MAC匹配:" + (ASPxMemo2.Text.IndexOf(ipmodel.macaddress==null?"*":ipmodel.macaddress) > 0 ? "是" : "否")+"\r\n");
                        }
                        ASPxMemo1.Text = sb1.ToString();
                    }
                }
            }

        }

        protected void ASPxRadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (ASPxRadioButtonList2.SelectedItem.Value.ToString() == "1")
            {
                ASPxTextBox1.Visible = true;
                ASPxMemo1.Visible = false;
            }
            else if (ASPxRadioButtonList2.SelectedItem.Value.ToString() == "2")
            {
                ASPxTextBox1.Visible = false;
                ASPxMemo1.Visible = true;
            }
        }

        protected void ASPxButton4_Click(object sender, EventArgs e)
        {
            //单个模式
            if (ASPxRadioButtonList2.SelectedItem.Value.ToString() == "1")
            {
                if (Maticsoft.Common.PageValidate.IsIp(ASPxTextBox1.Text.Trim()))
                {
                    string ip = ASPxTextBox1.Text.Trim();
                    ASPxMemo2.Text = string.Format("IP地址:{0}    连通状态:{1}    MAC地址:{2}", ip, (Maticsoft.Common.netping.pingIP(ip) == true ? "是" : "否"), Maticsoft.Common.netping.getMAC(ip));
                    new BLL.useroperatelogBll().AddLog(Session["username"].ToString(), "mac", "1个IP地址");
                }
            }
            else
            {
                //批量模式
                if (ASPxMemo1.Text.Trim().ToString().IndexOf("\r\n") > 0)
                {
                    string[] ips = Regex.Split(ASPxMemo1.Text.Trim().ToString(), @"\r\n");
                    StringBuilder sb = new StringBuilder();
                    Model.pingModel pingmodel = new Model.pingModel();
                    int pingpass = 0;
                    foreach (string ip in ips)
                    {
                        if (Maticsoft.Common.PageValidate.IsIp(ip))
                        {
                            pingmodel.IP = ip;
                            pingmodel.ping = Maticsoft.Common.netping.pingIP(pingmodel.IP);
                            if (pingmodel.ping)
                            {
                                pingpass++;
                                sb.Append(string.Format("IP地址:{0}    连通状态:{1}    MAC地址:{2}\r\n", ip, "是", Maticsoft.Common.netping.getMAC(ip)));
                            }

                        }
                        else
                        {
                            sb.Append(string.Format("IP地址:{0}    无效IP\r\n", ip));
                        }
                    }
                    sb.Append(string.Format("本次一共探测{0}个IP    {1}个连通", ips.Length, pingpass));
                    ASPxMemo2.Text = sb.ToString();
                    new BLL.useroperatelogBll().AddLog(Session["username"].ToString(), "mac", string.Format("{0}个IP地址", ips.Length));
                }
                else
                {
                    string[] ips = ASPxMemo1.Text.Trim().ToString().Split('-');
                    StringBuilder sb = new StringBuilder();
                    if (Maticsoft.Common.PageValidate.IsIp(ips[0].ToString()))
                    {
                        int i = int.Parse(ips[0].Split('.')[3].ToString());
                        int j = int.Parse(ips[1].ToString());
                        string iphead = ips[0].Split('.')[0].ToString() + "." + ips[0].Split('.')[1].ToString() + "." + ips[0].Split('.')[2].ToString() + ".";
                        if (i > j
                            || j > 255)
                        {
                            Maticsoft.Common.MessageBox.Show(this, "输入有错误!重新输入!");
                        }
                        else
                        {
                            string ip;
                            bool ping;
                            int pingpass = 0;
                            for (int k = i; k <= j; k++)
                            {
                                ip = iphead + k;
                                ping = Maticsoft.Common.netping.pingIP(ip);
                                if (ping)
                                {
                                    pingpass++;
                                    sb.Append(string.Format("IP地址:{0}    连通状态:{1}    MAC地址:{2}\r\n", ip, "是", Maticsoft.Common.netping.getMAC(ip)));
                                }

                            }
                            sb.Append(string.Format("本次一共探测{0}个IP    {1}个连通", j - i + 1, pingpass));
                            ASPxMemo2.Text = sb.ToString();
                            new BLL.useroperatelogBll().AddLog(Session["username"].ToString(), "mac", string.Format("{0}个IP地址", j - i + 1));
                        }
                    }
                    else
                    {
                        Maticsoft.Common.MessageBox.Show(this, "无效IP");
                    }
                }
            }

        }

        protected void ASPxButton3_Click(object sender, EventArgs e)
        {
            //单个的PING
            if (ASPxRadioButtonList2.SelectedItem.Value.ToString() == "1")
            {

                if (Maticsoft.Common.PageValidate.IsIp(ASPxTextBox1.Text.Trim()))
                {
                    Model.pingModel pingmodel = new Model.pingModel();
                    pingmodel.IP = ASPxTextBox1.Text.Trim();
                    pingmodel.ping = Maticsoft.Common.netping.pingIP(pingmodel.IP);
                    pingmodel.mac = "";
                    ASPxMemo2.Text = string.Format("IP地址:{0}    连通状态:{1}", pingmodel.IP, (pingmodel.ping == true ? "是" : "否"));
                    new BLL.useroperatelogBll().AddLog(Session["username"].ToString(), "ping", "1个IP地址");
                }
                else
                {
                    Maticsoft.Common.MessageBox.Show(this, "IP地址无效!");
                }
            }
            else
            {
                if (ASPxMemo1.Text.Trim().ToString().IndexOf("\r\n") > 0)
                {
                    string[] ips = Regex.Split(ASPxMemo1.Text.Trim().ToString(), @"\r\n");
                    StringBuilder sb = new StringBuilder();
                    Model.pingModel pingmodel = new Model.pingModel();
                    int pingpass = 0;
                    foreach (string ip in ips)
                    {
                        if (Maticsoft.Common.PageValidate.IsIp(ip))
                        {
                            pingmodel.IP = ip;
                            pingmodel.ping = Maticsoft.Common.netping.pingIP(pingmodel.IP);
                            if (pingmodel.ping)
                            {
                                pingpass++;
                                sb.Append(string.Format("IP地址:{0}    连通状态:{1}\r\n", pingmodel.IP, "是"));
                            }

                        }
                        else
                        {
                            sb.Append(string.Format("IP地址:{0}    无效IP\r\n", ip));
                        }
                    }
                    sb.Append(string.Format("本次一共探测{0}个IP    {1}个连通", ips.Length, pingpass));
                    ASPxMemo2.Text = sb.ToString();
                    new BLL.useroperatelogBll().AddLog(Session["username"].ToString(), "ping", string.Format("{0}个IP地址", ips.Length));
                }
                else
                {
                    string[] ips = ASPxMemo1.Text.Trim().ToString().Split('-');
                    StringBuilder sb = new StringBuilder();
                    if (Maticsoft.Common.PageValidate.IsIp(ips[0].ToString()))
                    {
                        int i = int.Parse(ips[0].Split('.')[3].ToString());
                        int j = int.Parse(ips[1].ToString());
                        string iphead = ips[0].Split('.')[0].ToString() + "." + ips[0].Split('.')[1].ToString() + "." + ips[0].Split('.')[2].ToString() + ".";
                        if (i > j
                            || j > 255)
                        {
                            Maticsoft.Common.MessageBox.Show(this, "输入有错误!重新输入!");
                        }
                        else
                        {
                            string ip;
                            bool ping;
                            int pingpass = 0;
                            for (int k = i; k <= j; k++)
                            {
                                ip = iphead + k;
                                ping = Maticsoft.Common.netping.pingIP(ip);
                                if (ping)
                                {
                                    pingpass++;
                                    sb.Append(string.Format("IP地址:{0}    连通状态:{1}\r\n", ip, "是"));
                                }

                            }
                            sb.Append(string.Format("本次一共探测{0}个IP    {1}个连通", j - i + 1, pingpass));
                            ASPxMemo2.Text = sb.ToString();
                            new BLL.useroperatelogBll().AddLog(Session["username"].ToString(), "ping", string.Format("{0}个IP地址", j - i + 1));
                        }
                    }
                    else
                    {
                        Maticsoft.Common.MessageBox.Show(this, "无效IP");
                    }
                }
            }
        }


    }
}